package com.extension.company.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.INotificationSideChannel;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.extension.company.admin.R;
import com.extension.company.admin.wxapi.GeneralizeShareActivity;
import com.extension.company.model.entity.HotDataListBean;
import com.extension.company.model.api.RetrofitUrlApi;
import com.extension.company.view.home.ImageShareActivity;

import java.util.List;

/**
 * fragmentHome界面的適配器
 */
public class HomeAdapter extends BaseAdapter implements View.OnClickListener {

	private Context context;
	private List<HotDataListBean.DataBean.HotDataBean> list;

	public HomeAdapter(Context context, List<HotDataListBean.DataBean.HotDataBean> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return null == list ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.activitylist_item, null);
			viewHolder.shareImg = convertView.findViewById(R.id.fraghome_share);
			viewHolder.productImg = convertView.findViewById(R.id.hot_item_img);
			viewHolder.saleGet = convertView.findViewById(R.id.hot_item_get);
			viewHolder.saleNum = convertView.findViewById(R.id.hot_item_sale_num);
			viewHolder.salePrice = convertView.findViewById(R.id.hot_item_sale);
			viewHolder.des = convertView.findViewById(R.id.hot_item_des);
			viewHolder.title = convertView.findViewById(R.id.hot_item_title);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.salePrice.setText(list.get(position).getGoods_price());
		viewHolder.saleNum.setText(String.valueOf(list.get(position).getSale()));
		viewHolder.saleGet.setText(String.format(context.getResources().getString(R.string.share_get), list.get(position).getShare_price()));
		viewHolder.title.setText(list.get(position).getGoods_name());
		viewHolder.des.setText(list.get(position).getDesc());
		Glide.with(context).load(RetrofitUrlApi.BaseHostUrl + list.get(position).getImage()).into(viewHolder.productImg);
		viewHolder.shareImg.setOnClickListener(this);


		return convertView;
	}

	static class ViewHolder {
		ImageView shareImg, productImg;
		TextView title, salePrice, saleGet, saleNum, des;
	}

	@Override
	public void onClick(View v) { // item中的分享界面
//		GeneralizeShareActivity.Internet((Activity) context);
		Intent intent = new Intent(context, ImageShareActivity.class);
		context.startActivity(intent);
	}

}
